package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.support.annotation.LayoutRes;
import android.view.View;

import com.android.databinding.library.baseAdapters.BR;

import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;

/**
 * Created by zxh on 16/12/11.
 */

public class SearchItem extends BindingListItem {
    private int id;
    private int click;
    private String title;
    private String content;
    private String nickName;
    private String img;
    private String type;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Bindable
    public int getClick() {
        return click;
    }

    public void setClick(int click) {
        this.click = click;
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Bindable
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Bindable
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Bindable
    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public SearchItem(Context context, @LayoutRes int layoutId, String type) {
        super(context, layoutId);
        this.type = type;
    }

    public void onClickItem(View view) {
        CommonFragment fragment = null;
        switch (type) {
            case "w":
                ArticleItem articleItem = new ArticleItem(getContext(), 0);
                articleItem.setArticleId(id);
                ArticleDetail articleDetail = new ArticleDetail(getContext(), articleItem);
                fragment = new CommonFragment();
                fragment.setVariableId(BR.detail);
                fragment.setLayoutId(R.layout.f_article_detail);
                fragment.setDataModel(articleDetail);
                HomeActivity.switchFragment((Activity) getContext(), fragment);
                break;
            case "v":
                // 打开视频详情
                final CommonFragment videoFragment = HomeActivity.openVideoDetail(getContext(), id, null);
                LoadingDialog.getInstance(getContext()).show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            VideoDetail detail = WebApi.getVideoDetail(getContext(), id);
                            videoFragment.setDataModel(detail);
                        } catch (Exception ex) {

                        }
                        LoadingDialog.getInstance(getContext()).dismiss();
                    }
                }).start();
                break;
            case "":
                break;
        }
    }
}
